<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Hook;
use think\Db;

/**
 * 公共接口
 */
class Common extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {

        if (isset($_SERVER['HTTP_ORIGIN'])) {
            header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
        }
        //跨域检测
        check_cors_request();

        if (!isset($_COOKIE['PHPSESSID'])) {
            Config::set('session.id', $this->request->server("HTTP_SID"));
        }
        parent::_initialize();
    }

    /**
     * 上传文件
     * @ApiMethod (POST)
     * @ApiParams (name="file", type="file", required=true, description="文件流")
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            $savekey = $this->request->post('savekey',null);
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload($savekey);
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
        }

    }

    // 获取启动页图片
    public function getStartImageList(){
        $list = \app\common\model\content\AdvertStartImage::order('weigh','desc')->select();
        $this->success('',$list);
    }

    // 获取banner
    public function getBannerList(){
        $type = $this->request->get('type') ?? 10;
        $list = \app\common\model\content\Banner::where('type',$type)->order('weigh','desc')->select();
        $this->success('',$list);
    }

    // 获取公告列表
    public function getNoticeList(){
        $type = $this->request->get('type') ?? 10;
        $list = \app\common\model\content\Notice::where('type','in',[$type,0])->order('createtime','desc')->select();
        $this->success('',$list);
    }

    // 获取公告详情
    public function getNoticeDetail(){
        $id = $this->request->get('id') ?? '';
        if(!$id){
            $this->error('参数错误');
        }
        $info = \app\common\model\content\Notice::get($id);
        $this->success('',$info);
    }

    // 获取协议列表
    public function getArticleList(){
        $status = $this->request->get('status',20);
        $list = \app\api\model\content\CustomArticle::where('status',$status)->order('weigh','desc')->select();
        $this->success('',$list);
    }

    // 获取协议详情
    public function getArticleDetail(){
        $title = $this->request->get('title') ?? '';
        $id = $this->request->get('id') ?? '';
        if(!$title && !$id){
            $this->error('参数错误');
        }
        $where = [];
        if($id){
            $where['id'] = $id;
        }else{
            $where['title'] = $title;
        }
        $info = \app\api\model\content\CustomArticle::where($where)->find();
        $this->success('',$info);
    }

    /**
     * 获取充值套餐列表
     */
    public function getRechargePackageList(){
        $list = \app\api\model\matketing\RechargePackage::order('weigh','desc')->select();
        $this->success('',$list);
    }

    /**
     * 问题反馈类型
     */
    public function getMessageTypeList(){
        $list = \app\api\model\message\MessageType::order('weigh','desc')->select();
        $this->success('',$list);
    }

    /**
     * 获取医院列表
     */
    public function getHospitalList(){
        $list = \app\api\model\hospital\Hospital::select();
        $this->success('',$list);
    }

    public function getSysConfig(){
        $wxapp_logo = Config::get('site.wxapp_logo');
        $this->success('',['wxapp_logo'=>cdnurl($wxapp_logo)]);
    }

}
